<?php
/***********************************************************************
 * @package    IvanEngine
 * @subpackage Core
 * @author     Ivan <ivanzx@msn.com>
 * @created    2010-05-12
 ***********************************************************************/
if(defined("IVANENGINE_CORE_TIME"))
	die("Redefinition macro in :".__FILE__."(".__LINE__.")");
define("IVANENGINE_CORE_TIME", true);

require_once (dirname(__FILE__).'/Common.php');

class CTime extends CBase
{
	function __construct()
	{
		parent::__construct();
	}

	function __destruct()
	{
		parent::__destruct();
	}
	
	// $arr = getdate();
	// echo "Date is " . $arr['mday'] . " " . $arr['weekday'] . " " . $arr['year'];
	// echo "Time is " . $arr['hours'] . ":" . $arr['minutes'];
	static function GetDate($timestamp = null)
	{
		if( $timestamp != null )
			return getdate($timestamp);
		else
			return getdate();
	}
	
	// echo checkdate(2,30,2005) ? "valid" : "invalid"; returns false
	// echo checkdate(4,6,2010) ? "valid" : "invalid"; returns true
	static function CheckDate($month, $day, $year)
	{
		return checkdate($month, $day, $year);
	}
	
	// echo mktime(13,15,23,6,7,2006);
	// returns timestamp for 13:15:23 7-Jun-2006
	static function MakeTime($hour = null, $minute = null, $second = null, 
		$month = null, $day = null, $year = null, $is_dst = null)
	{
		return mktime($hour, $minute, $second, $month, $day, $year, $is_dst);
	}
	
	// echo date("d-M-Y h:i A", mktime());
	// returns "13-Sep-2005 01:16 PM"
	static function Date($format, $timestamp = null)
	{
		if( $timestamp != null )
			return date($format, $timestamp);
		else
			return date($format);
	}
	
	// echo date("d-M-y", strtotime("tomorrow")); returns 14-Sep-05
	// echo date("d-M-y", strtotime("today +3 days")); returns 16-Sep-05
	static function StrToTime($strTime, $now = null)
	{
		return strtotime($strTime, $now);
	}
	
	// format month/day names
	// echo strftime("Month: %B "); 
	// echo strftime("Day: %A ")
	// as per locale setting, returns "septembre" and "mardi"
	static function StrFTime($format, $timestamp = null)
	{
		if( $timestamp != null )
			return strftime($format, $timestamp);
		else
			return strftime($format);
	}
	
	// calculate time taken for code execution
	// $start = microtime(); ... $end = microtime();
	// echo "Elapsed time: " . ($end - $start) ." sec";
	static function MicroTime(bool $get_as_float = null)
	{
        list($usec, $sec) = explode(" ",microtime($get_as_float)); 
        return ((float)$usec + (float)$sec); 
	}
	
	// echo gmmktime(12,25,23,7,9,2006);
	// returns timestamp for 12:25:23 9-Jul-2006
	static function GMMKTime($hour = null, $minute = null, $second = null, 
		$month = null, $day = null, $year = null, $is_dst = null)
	{
		return gmmktime($hour, $minute, $second, $month, $day, $year, $is_dst);
	}
	
	// echo gmdate("d-M-Y h:i A", mktime())
	// returns "13-Sep-2005 08:32 AM"
	static function GMDate($format, $timestamp = null)
	{
		if( $timestamp != null )
			return gmdate($format, $timestamp);
		else
			return gmdate($format);
	}
	
	// date_default_timezone_set('UTC');
	// the function only support PHP 5.1+
	static function SetTimeZone($timezone_identifier)
	{
		date_default_timezone_set($timezone_identifier);
	}
	static function GetTimeZone()
	{
		return date_default_timezone_get();
	}
}

?>